package com.atguigu.day07;

import com.atguigu.bean.Event;
import com.atguigu.day03.Flink05_Source_Custom;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

public class Flink03_SplitStreamByFilter_WithSideOutput {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //2.获取数据
        DataStreamSource<Event> streamSource = env.addSource(new Flink05_Source_Custom.ClickSource());

        //TODO 3.使用侧输出流将一条流根据不同的用户拆分成多条流
        SingleOutputStreamOperator<Event> result = streamSource.process(new ProcessFunction<Event, Event>() {
            @Override
            public void processElement(Event value, ProcessFunction<Event, Event>.Context ctx, Collector<Event> out) throws Exception {
                if ("Bingbing".equals(value.user)) {
                    ctx.output(new OutputTag<Event>("Bingbing") {
                    }, value);
                } else {
                    out.collect(value);
                }

            }
        });


        result.print("Songsong");
        //获取到侧输出流的数据
        result.getSideOutput(new OutputTag<Event>("Bingbing") {
        }).print("Bingbing");

        env.execute();
    }
}
